<template>
  <div>
    <div v-if="users.length > 0">
      <div class="title_content">
        <div class="avatar">头像</div>
        <div class="name">昵称</div>
        <div class="time">入团时间</div>
      </div>
      <div class="content" v-for="(user, index) in users" :key="index">
        <img class="img" :src="user.image" alt="">
        <div class="nickname">{{ user.username }}</div>
        <div class="timeuser">{{ user.createtime }}</div>
        <u-line></u-line>
      </div>
    </div>
    <u-empty v-else mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
    <u-loadmore :status="status" @loadmore="loadMoreData"></u-loadmore>
  </div>
</template>

<script>
import { getTeamList } from "@/api/apiService.js";

export default {
  data() {
    return {
      users: [],
      pagination: {
        page: 1
      },
      status: 'loadmore',
    };
  },

  created() {
    this.fetchData();
  },

  methods: {
    async fetchData() {
      if (this.status === 'loading') return;
      this.status = 'loading';

      const params = {
        page: this.pagination.page
      };

      try {
        const response = await getTeamList(params);
        const newUsers = response.data?.list || [];
        if (newUsers.length > 0) {
          this.users = [...this.users, ...newUsers];
          this.status = 'loadmore';
        } else {
          if (response.data.length === 0) {
            this.status = 'nomore';
          } else {
            this.status = 'loadmore';
          }
        }
      } catch (error) {
        console.error("Failed to fetch data:", error);
      }
    }
  },

  onReachBottom() {
    if (this.status === 'loadmore') {
      this.pagination.page += 1;
      this.fetchData();
    }
  },
};
</script>

<style scoped>
page {
  background-color: #F6F6F6;
}

.timeuser {
  padding: 15px;
  font-family: Source Han Sans-Normal;
  color: #999999;
  font-size: 11px;
}

.nickname {
  padding: 15px;
  font-family: Source Han Sans-Normal;
}

.img {
  padding: 15px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.title_content {
  display: flex;
  justify-content: space-between;
  width: 100%;
  background-color: #EEF1FF;
  font-size: 13px;
}

.content {
  display: flex;
  justify-content: space-between;
  width: 100%;
  background-color: white;
  font-size: 13px;
  align-items: center;
  flex-wrap: wrap;
}

.avatar {
  padding: 15px 20px 0px 30px;
  font-family: Source Han Sans-Normal;
}

.name {
  padding: 15px 25px;
  font-family: Source Han Sans-Normal;
}

.time {
  padding: 15px 20px 0px 20px;
  font-family: Source Han Sans-Normal;
}

.loading-text,
.no-more-text {
  text-align: center;
  padding: 10px;
  color: #666;
}
</style>
